<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="团队名称">
              <select-team-input v-model="queryParam.teamId" :teamType="0" :tdlx="3" multi/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('资金调拨明细')" :disabled="isDisabledAuth('ErpCapitalAllotReportList:export')">导出</a-button> 
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:scrollX}"
        :components="draggableComponent"
        bordered
        rowKey="teamId"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :rowSelection="{getCheckboxProps:getCheckboxProps,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="moneyjan" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a @click="getCapitalAllot(record,'jan')">{{ text }}</a>
        </span>
        <span slot="moneyfeb" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'feb')">{{ text }}</a>
        </span>
        <span slot="moneymar" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'mar')">{{ text }}</a>
        </span>
        <span slot="moneyapr" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'apr')">{{ text }}</a>
        </span>
        <span slot="moneymay" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'may')">{{ text }}</a>
        </span>
        <span slot="moneyjun" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'jun')">{{ text }}</a>
        </span>
        <span slot="moneyjul" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'jul')">{{ text }}</a>
        </span>
        <span slot="moneyaug" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'aug')">{{ text }}</a>
        </span>
        <span slot="moneysept" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'sept')">{{ text }}</a>
        </span>
        <span slot="moneyoct" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'oct')">{{ text }}</a>
        </span>
        <span slot="moneynov" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'nov')">{{ text }}</a>
        </span>
        <span slot="moneydec" slot-scope="text, record">
          <span v-if="record.rowIndex == '合计（元）'">{{ text }}</span>
          <a v-else @click="getCapitalAllot(record,'dec')">{{ text }}</a>
        </span>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>
    <ErpCapitalAllot ref="capitalAllot" />
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import { deleteAction, downFile, getAction, getFileAccessHttpUrl } from '@/api/manage'
  import moment from 'moment'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import ErpCapitalAllot from './ErpCapitalAllot'

  export default {
    name: 'ErpCapitalAllotReportList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      SelectTeamInput,
      ErpCapitalAllot
    },
    data () {
      return {
        ipagination: {
          current: 1,
          pageSize: 99999999,
          pageSizeOptions: ['10', '20', '30', '50', '100', '200'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        description: '资金调拨明细',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            ellipsis: true,
            customRender:function (text,r,index) {
              return (!text.rowIndex) ? (parseInt(index) + 1) : text.rowIndex;
            }
          },
          {
            title:'主负责人',
            align:"center",
            dataIndex: 'mainLeader_dictText',
            ellipsis: true,
            width:100,
          },
          {
            title:'团队名称',
            align:"center",
            dataIndex: 'teamId_dictText',
            ellipsis: true,
            width:100,
          },
          {
            title:'1月',
            align:"center",
            dataIndex: 'jan',
            ellipsis: true,
            scopedSlots: { customRender: 'moneyjan' },
            width:100,
          },
          {
            title:'2月',
            align:"center",
            dataIndex: 'feb',
            scopedSlots: { customRender: 'moneyfeb' },
            ellipsis: true,
            width:100,
          },
          {
            title:'3月',
            align:"center",
            dataIndex: 'mar',
            scopedSlots: { customRender: 'moneymar' },
            ellipsis: true,
            width:100,
          },
          {
            title:'4月',
            align:"center",
            dataIndex: 'apr',
            scopedSlots: { customRender: 'moneyapr' },
            ellipsis: true,
            width:100,
          },
          {
            title:'5月',
            align:"center",
            dataIndex: 'may',
            scopedSlots: { customRender: 'moneymay' },
            ellipsis: true,
            width:100,
          },
          {
            title:'6月',
            align:"center",
            dataIndex: 'jun',
            scopedSlots: { customRender: 'moneyjun' },
            ellipsis: true,
            width:100,
          },
          {
            title:'7月',
            align:"center",
            dataIndex: 'jul',
            scopedSlots: { customRender: 'moneyjul' },
            ellipsis: true,
            width:100,
          },

          {
            title:'8月',
            align:"center",
            dataIndex: 'aug',
            scopedSlots: { customRender: 'moneyaug' },
            ellipsis: true,
            width:100,
          },
          {
            title:'9月',
            align:"center",
            dataIndex: 'sept',
            scopedSlots: { customRender: 'moneysept' },
            ellipsis: true,
            width:100,
          },
          {
            title:'10月',
            align:"center",
            dataIndex: 'oct',
            scopedSlots: { customRender: 'moneyoct' },
            ellipsis: true,
            width:100,
          },
          {
            title:'11月',
            align:"center",
            dataIndex: 'nov',
            scopedSlots: { customRender: 'moneynov' },
            ellipsis: true,
            width:100,
          },
          {
            title:'12月',
            align:"center",
            dataIndex: 'dec',
            scopedSlots: { customRender: 'moneydec' },
            ellipsis: true,
            width:100,
          },
          {
            title:'合计（元）',
            align:"center",
            dataIndex: 'sumAmount',
            ellipsis: true,
            width:100,
          },
        ],
        url: {
          list: "/api/erp/finance/report/erpCapitalAllotReport/list", 
          exportXlsUrl: "/api/erp/finance/report/erpCapitalAllotReport/exportXls",
        },
        //代码生成后需手动设置流程编码
        dictOptions:{},
        superFieldList:[],
        newArr:[],
        newDataSource:[],
      }
    },
    mounted() {
      /*新增分页合计方法*/
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      // 打开资金调拨申请单明细弹窗
      getCapitalAllot(record,key) {
        //先清空数据
        this.$refs.capitalAllot.dataSource = []
        this.$refs.capitalAllot.ipagination.total = 0
        //查询数据
        this.$refs.capitalAllot.loading = true
        this.$refs.capitalAllot.queryParam.allotInTeam = record.teamId
        this.$refs.capitalAllot.queryParam.searchMonth = key
        this.$refs.capitalAllot.searchReset()

        //打开弹窗
        this.$refs.capitalAllot.visible = true
      },
      searchQuery() {
        this.onClearSelected();
        this.loadData(1)
      },
      searchReset(){
        this.queryParam = {}
        this.onClearSelected();
        this.loadData(1)
      },
      //流程审批结束禁用
      getCheckboxProps(record) {
        return {
          props: {
            disabled: record.rowIndex =='合计' 
          }
        }
      },
      loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //判断是否当前页没有数据且不为第一页
          if (this.dataSource.length == 0 && this.ipagination.current != 1) {
            this.ipagination.current = this.ipagination.current - 1
            this.loadData(this.ipagination.current - 1);
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.tableAddTotalRow(this.columns, this.dataSource)
        } else {
          this.dataSource = [];
          this.ipagination.total = 0
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
      /** 表格增加合计行 */
      tableAddTotalRow(columns, dataSource) {
        let numKey = 'rowIndex'
        let totalRow = { [numKey]: '合计（元）' }
        columns.forEach(column => {
          let { key, dataIndex } = column
          if (![key, dataIndex].includes(numKey)) {
            let total = 0
            dataSource.forEach(data => {
              total += /^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test(data[dataIndex]) ? Number.parseFloat(data[dataIndex]) : Number.NaN
              console.log(data[dataIndex], ':', (/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test(data[dataIndex]) ? Number.parseFloat(data[dataIndex]) : Number.NaN))
            })
            if (Number.isNaN(total)) {
              total = '-'
              totalRow[dataIndex] = total;
            }else{
              totalRow[dataIndex] = total.toFixed(2);
            }
            
          }
        })
        console.log(totalRow)
        dataSource.push(totalRow)
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'date',value:'createTime',text:'创建日期'})
        fieldList.push({type:'string',value:'handlerId',text:'经办人编号',dictCode:''})
        fieldList.push({type:'string',value:'handlerDeptId',text:'经办人部门ID',dictCode:''})
        fieldList.push({type:'string',value:'billNo',text:'单据编号',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'applicant',text:'申请公司',dictCode:''})
        fieldList.push({type:'string',value:'isRelContract',text:'是否关联合同',dictCode:'yes_no'})
        fieldList.push({type:'string',value:'payMode',text:'支付方式(字典)',dictCode:'payment_type'})
        fieldList.push({type:'string',value:'payeeName',text:'收款方名称',dictCode:''})
        fieldList.push({type:'string',value:'payeeBankName',text:'收款方开户行',dictCode:''})
        fieldList.push({type:'string',value:'payeeAccountNo',text:'收款方账户',dictCode:''})
        fieldList.push({type:'string',value:'payeeContacts',text:'收款联系人',dictCode:''})
        fieldList.push({type:'string',value:'payeeContactInfo',text:'收款联系方式',dictCode:''})
        fieldList.push({type:'string',value:'payRemark',text:'打款备注',dictCode:''})
        fieldList.push({type:'Text',value:'attachments',text:'附件',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
